//页面首次创建时显示数据
$("#product_index").on('pagebeforecreate',function(){
	//输入搜索条件自动查询数据
	$("#product_index").on( "change","#product_name", function(event, ui) {
		$("#form_search :input[name='name']").val($("#product_name").val()) ;
		$("#productListView").html("");
		$("#form_search").attr("action","/product/list.html");
		searchProduct();	
	});
	
	//输入搜索条件自动查询数据
	$("#product_index").on( "click","#morePageBtn", function(event, ui) {
		searchProduct();	
	});
});



$("#product_index").on('pageshow',function(){
	if($("#productListView li").length<=0){
		searchProduct();
	}
});
function searchProduct(){
	$.ajax({
        'url': $("#form_search").attr("action"),
        'dataType': "json",
        'data': $('#form_search').serialize(),
        'success': function (data) {
        	var list = data['list'];
        	var page = data['page'];
        	$.each(list,function(i,item){
        		$(".productTmpl").tmpl(item).appendTo("#productListView");
        	});
        	$("#form_search").attr("action",page['downPageUrl']);
        	if(page['totalPages']==page['nowPage']){
        		$("#morePageBtn").hide();
        	}else{
        		$("#morePageBtn").show();
        	}
        }
    });
}

function addToCart(code,num){
	//问题 直接在底部不加载数据了
	$.ajax({
		'async': false, 
        'url': "/cart/doadd.html",
        'dataType': "json",
        'data': {'code':code,'num':num},
        'success': function (data) {
        	var cart_num = data['cart_num'];
        	var item_num = data['item_num'];
        }
    });
}

function isAtBottom(){
	var totalHeight, currentScroll, visibleHeight;
	if (document.documentElement.scrollTop){ 
		currentScroll = document.documentElement.scrollTop; 
	}else{ 
		currentScroll = document.body.scrollTop; 
	}
	totalHeight = document.body.offsetHeight;
	visibleHeight = document.documentElement.clientHeight;
	return (totalHeight <= currentScroll + visibleHeight )
}